/***************************************************************************
 *   Copyright (C) 2009 by betox2099   *
 *   betox2099@yahoo.com.mx   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "Game.h"


Game::Game(CL_DisplayWindow win)
{
	b_stop = 0;
	window = win;
	input_device = window.get_ic().get_keyboard();
	graphic_context = window.get_gc();
	background_color = CL_Colorf::black;
	init();
}

Game::~Game(){}

void Game::update()
{
	CL_Console::write_line("Actualizando");
}

void Game::manageInputs()
{
	CL_Console::write_line("Leyendo inputs");
	if(input_device.get_keycode(CL_KEY_ESCAPE)){
		stop();
	}
}

void Game::render()
{
	std::vector<Drawable *>::iterator iterador;
	Drawable *object_draw;

	CL_Console::write_line("Render");
	graphic_context.clear(background_color);
	
	if((int)drawable_objects.size() > 0){
		iterador = drawable_objects.begin();
		while(iterador != drawable_objects.end()){
			object_draw = (Drawable *)*iterador;
			object_draw->draw(graphic_context);		
			++iterador;	
		}
	}
	window.flip();	
}

void Game::run()
{
	while(!b_stop){
		manageInputs();
		CL_KeepAlive::process();
		update();
		render();
		CL_System::sleep(100);
	}
}

void Game::init()
{
}

void Game::start()
{
	b_stop = 0;
	thread.start(this,
                     &Game::run);
	thread.join();
}

void Game::stop()
{
	b_stop = 1;
}
